﻿var globalFrameHeight = 0;
$(function () {
	//隐藏左面菜单
	$(".menu-toggle").click(function () {
		if ($(".main-left").css("display") == "none") {
			$(".main-left").show();
			$(".container-menu").show("slide");
		} else {
			$(".main-left").fadeOut();
		}
	});
	loadMenu();
	calculateWindowSize();
	$(window).resize(function () { calculateWindowSize(); });
	$(document).keydown(menuShortKeydown).keyup(menuShortKeyup);
});

/* ctrl & esc 快捷键Keydown事件
----------------------------------------------------------*/
var _bind_ctrl_keyup_event = true;
function menuShortKeydown(e) {
	if (e.ctrlKey && e.keyCode != 17) {
		_bind_ctrl_keyup_event = false;
		return;
	}
}

/* ctrl & esc 快捷键Keyup事件
----------------------------------------------------------*/
function menuShortKeyup(e) {
	if (!_bind_ctrl_keyup_event)
		return;
	if (e.keyCode == 27) {
		var hasExpendSecond = false;
		$(window.top.document).find(".container-menu dl").each(
					function () {
						if (hasExpendSecond)
							return;
						hasExpendSecond = $(this).css("display") == "block";
					}
				);
		if (hasExpendSecond) {
			$(window.top.document).find(".container-menu dl").hide();
		}
		else {
			$(window.top.document).find(".main-left").fadeOut();
		}
	}
	if (e.keyCode == 17) {
		if ($(window.top.document).find(".main-left").css("display") == "none") {
			$(window.top.document).find(".main-left").show();
			$(window.top.document).find(".container-menu").show("slide");
			return;
		}
		else {
			if ($(window.top.document).find(".container-menu .dt-selected").length == 0) {
				$(window.top.document).find(".container-menu .second-level-menu").first().click();
			} else {
				$(window.top.document).find(".container-menu .dt-selected").click();
			}
		}
	}
}

/* 一级菜单点击事件
----------------------------------------------------------*/
function topMenuClick(menu) {

	/* 把所有标签改成非活动
	----------------------------------------------------------*/
	var activeTab = $(".menu-active");
	var zIndex = activeTab.css("z-index") / 10;
	activeTab.css("z-index", zIndex);
	activeTab.attr("class", "menu-inactive");

	/* 显示活动标签
	----------------------------------------------------------*/
	var objSelectedLevel1Menu = $("#" + menu);
	if (objSelectedLevel1Menu.attr("class") == "menu-active") {
		return;
	}
	else {
		objSelectedLevel1Menu.attr("class", "menu-active");
		zIndex = objSelectedLevel1Menu.css("z-index") * 10;
		objSelectedLevel1Menu.css("z-index", zIndex);
	}
}

/* 加载菜单脚本
----------------------------------------------------------*/
function loadMenu() {
	/* 所有二级菜单隐藏
	----------------------------------------------------------*/
	var menuId = ".container-menu";
	$(menuId).find("dl").hide();

	/* 点击事件
	----------------------------------------------------------*/
	$(menuId).find(".second-level-menu").filter(function () { return $(this).parent().find("dd").size() != 0 }).click(function () {
		if ($(this).parent().find("dl").css("display") == "block") {
			$(this).parent().find("dl").fadeOut();
		} else {
			$(this).parent().find("dl").height($(".container-menu").height());
			$(".container-menu .second-level-menu").removeClass("on").removeClass("nosmon").removeClass("dt-selected");
			$(".container-menu dl").hide();
			$(this).addClass("on").addClass("dt-selected");
			$(this).parent().find("dl").show("slide");
		}
	})

	/* 二级菜单点击样式
	----------------------------------------------------------*/
	$(menuId).find("dd").click(function () { $("dd").removeClass("focus"); $(this).addClass("focus"); });

}

/* 首页主体区域URL重定向
----------------------------------------------------------*/
function setframeUrl(url, menu) {
	$("#mainFrame").attr("src", url);
	$(".navigation span").html(menu);
	$(".container-menu dl").hide('fast');
}

/* 计算每部分内容尺寸
----------------------------------------------------------*/
function calculateWindowSize() {
	$("body").height($("html").height());
	var menuHeight = $("body").height() - $(".container-top").height();
	$(".main-content").height(menuHeight);
	$(".container-menu").height(menuHeight);
	$("#mainFrame").height(menuHeight - $(".navigation").height());
	globalFrameHeight = $("#mainFrame").height();
}


/* 打开Dialog
----------------------------------------------------------*/
/*
url
, title
, width
, height
, beforeClose
*/
function openDialog(option) {
	option = option || {};
	var _default = {
		index: "1",
		width: 650,
		height: 550
	};
	$.extend(_default, option);
	var dialogID = "#dialog" + _default.index;
	var frameID = "#iframeDialog" + _default.index;
	if ($(frameID)[0].contentWindow.itpOverlay)
		$(frameID)[0].contentWindow.itpOverlay.show();
	if ($.browser.msie)
		_default.height += 160;
	$(frameID).attr("src", _default.url);
	$(dialogID).dialog({ modal: true, title: _default.title, width: _default.width, height: _default.height, canMinimize: true, canMaximize: false, beforeClose: _default.beforeClose });
}

/* 关闭框架层
----------------------------------------------------------*/
/*
isRefreshParent
, message
, callback
, isShowMessage
, index
*/
function closeDialog(option) {
	option = option || {};
	var _default = {
		index: "1",
		isRefreshParent: false,
		isShowMessage: false,
		callback: ""
	};
	$.extend(_default, option);
	if (_default.refreshParent) {
		var mainFrame = document.getElementById("mainFrame");
		var contentWindow = $("#mainFrame")[0].contentWindow;
		if (contentWindow.commonPage.getCurrentUrl() != undefined && contentWindow.commonPage.getCurrentUrl() != null)
			contentWindow.location = contentWindow.commonPage.getCurrentUrl();
		else
			contentWindow.location = contentWindow.location;
	}

	if (_default.isShowMessage)
		sysMessage(_default.message);
	var dialogID = "#dialog" + _default.index;
	if (_default.callback != undefined && _default.callback != "")
		window.eval(_default.callback + '();');
	$(dialogID).dialog("close");
}


/* 刷新弹出层
----------------------------------------------------------*/
function refreshDialog(index) {
	$("#iframeDialog" + index).contents()[0].location = $("#iframeDialog" + index).contents()[0].location;
}

/* 关闭、刷新二级iframe  
二级iframe ID为：embeddedFrame
isRefreshParent
, message
, callback
, isShowMessage
, index
----------------------------------------------------------*/
function closeEmbeddedFrameDialog(option) {
	option = option || {};
	var _default = {
		index: "1",
		isRefreshParent: false,
		isShowMessage: false,
		callback: undefined
	};
	$.extend(_default, option);
	if (_default.refreshParent) {
		var contentWindow = $("#mainFrame").contents().find("#embeddedFrame")[0].contentWindow;
		if (contentWindow.commonPage.getCurrentUrl() != undefined && contentWindow.commonPage.getCurrentUrl() != null)
			contentWindow.location = contentWindow.commonPage.getCurrentUrl();
		else
			contentWindow.location = contentWindow.location;
	}
	if (!_default.isShowMessage)
		sysMessage(_default.message);
	var dialogID = "#dialog" + option.index;
	if (_default.callback != undefined && _default.callback != "")
		window.eval(_default.callback + '();');
	$(dialogID).dialog("close");
}

/* 右下角弹出系统消息
----------------------------------------------------------*/
function sysMessage(message) {
	if (message != undefined && message != "")
		$("#sysMessage").html(message);
	var left = $("html").width() - $("#sysMessage").outerWidth();
	var top = $("html").height() - $("#sysMessage").outerHeight() - 10;
	$("#sysMessage").css("left", left + "px");
	$("#sysMessage").css("top", top + "px");
	$("#sysMessage").css("display", "block");
	$("#sysMessage").slideDown();
	window.setTimeout("$('#sysMessage').fadeOut();", 2000);

}

/* 错误消息提示
----------------------------------------------------------*/
function sysAlert(message) {
	if (message == undefined)
		return;
	$("#sysAlert").html(message);
	$("#sysAlert").dialog({ modal: true, title: "系统提示", height: "200" });
	$("#sysAlert").dialog({
		modal: true,
		buttons: {
			确定: function () {
				$(this).dialog("close");
			}
		}
	});
}


var _temp_cache_key = null;
function setCacheValue(value) {
	var date = new Date();
	var ticks = date.getTime() + "_value";
	window[ticks] = value;
	_temp_cache_key = ticks;
}

function getCacheValue(isKeep) {
	var temp = window[_temp_cache_key];
	if (!isKeep)
		window[_temp_cache_key] = null;
	return temp;
}

var _temp_callback_key = null;
function setCallBackFun(fun) {
	var date = new Date();
	var ticks = date.getTime() + "_callback";
	window[ticks] = fun;
	_temp_callback_key = ticks;
}

function getCallBackFun(isKeep) {
	var temp = window[_temp_callback_key];
	if (!isKeep)
		window[_temp_callback_key] = null;
	return temp;
}






